#if !defined __SCRIPT_EDITOR_FRAME_H
#define __SCRIPT_EDITOR_FRAME_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define SCRIPT_MESSAGE_MAP 14

#include "FlatTabCtrl.h"
#include "WinUtil.h"
#include "RtfScriptEditor.h"
#include "ExListViewCtrl.h"
#include "../client/ScriptManager.h"

class ScriptEditorFrame : private ScriptManagerListener,
	public MDITabChildWindowImpl<ScriptEditorFrame>, 
	public StaticFrame<ScriptEditorFrame, ResourceManager::SCRIPT_EDITOR>
{
public:
	DECLARE_FRAME_WND_CLASS_EX(_T("ScriptEditorFrame"), IDR_LUA, 0, COLOR_3DFACE);

	typedef MDITabChildWindowImpl<ScriptEditorFrame> baseClass;

	ScriptEditorFrame() : padContainer(_T("edit"), this, SCRIPT_MESSAGE_MAP) { 
		ScriptManager::getInstance()->addListener(this); 
	}
	~ScriptEditorFrame() { 
		ScriptManager::getInstance()->removeListener(this);
		images.Destroy();
	}

	BEGIN_MSG_MAP(ScriptEditorFrame)
		MESSAGE_HANDLER(WM_SETFOCUS, OnFocus)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_CLOSE, onClose)
		MESSAGE_HANDLER(WM_CTLCOLORSTATIC, onCtlColor)
		MESSAGE_HANDLER(WM_CONTEXTMENU, onContextMenu)
		COMMAND_ID_HANDLER(IDC_OPEN_SCRIPT, onOpen)
		COMMAND_ID_HANDLER(IDC_SAVE_SCRIPT, onSave)
		COMMAND_ID_HANDLER(IDC_RELOAD_SCRIPT, onReloadScript)
		COMMAND_ID_HANDLER(IDC_RELOAD_PAD, onReloadPad)
		COMMAND_ID_HANDLER(IDC_NEW_SCRIPT, onNewScript)
		COMMAND_ID_HANDLER(ID_EDIT_CUT, onEditCut)
		COMMAND_ID_HANDLER(ID_EDIT_COPY, onEditCopy)
		COMMAND_ID_HANDLER(ID_EDIT_PASTE, onEditPaste)
		NOTIFY_HANDLER(IDC_SCRIPT_LIST, NM_DBLCLK, onDblClick)
		CHAIN_MSG_MAP(baseClass)
		REFLECT_NOTIFICATIONS()
	ALT_MSG_MAP(SCRIPT_MESSAGE_MAP)
		MESSAGE_HANDLER(WM_LBUTTONUP, onChange)
		MESSAGE_HANDLER(WM_KEYUP, onChange)
	END_MSG_MAP()

	virtual void OnFinalMessage(HWND /*hWnd*/) {
		delete this;
	}

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled);
	LRESULT onOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onReloadPad(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onReloadScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onNewScript(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT onCtlColor(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT onContextMenu(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
	LRESULT onChange(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
		setLineCount();
		return 0;
	}
	LRESULT OnFocus(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) {
		ctrlPad.SetFocus();
		return 0;
	}
	LRESULT onEditCut(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		ctrlPad.Cut();
		return 0;
	}
	LRESULT onEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		ctrlPad.Copy();
		return 0;
	}
	LRESULT onEditPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
		ctrlPad.Paste();
		return 0;
	}
	LRESULT onDblClick(int /*idCtrl*/, LPNMHDR /* pnmh */, BOOL& bHandled) {
		return onOpen(0, 0, 0, bHandled);
	}
	void UpdateLayout(BOOL bResizeBars = TRUE);
	void addLine(const string& s);

private:
	CRtfScriptEditorCtrl ctrlPad;
	CEdit ctrlDebug;
	CContainedWindow padContainer;
	LOGFONT scriptFont;
	COLORREF textColor, bgColor;
	HBRUSH bgBrush;
	CStatusBarCtrl ctrlStatus;
	ExListViewCtrl ctrlScripts;
	string currentFileName;
	CHorSplitterWindow horSplit;
	CSplitterWindow verSplit;
	CImageList images;

	void on(ScriptManagerListener::Types /*type*/, const string& aLine) throw() {
		addLine(aLine);
	}
	void setStatus();
	void setLineCount();
	void save();
	HWND createToolbar();
};

#endif __SCRIPT_EDITOR_FRAME_H